namespace RAYTRACING {
    /**
     * 对象池
     */
    export class Pool<T> {
        private static readonly inPoolTag = "__INPOOLTAG__";
        private _typeConstructor: { new(): T };
        private _list: T[] = [];
        constructor(typeConstructor: { new(): T }, initCount: number = 0) {
            if (initCount == null || initCount < 0) initCount = 0;
            this._typeConstructor = typeConstructor;
            for (let i = 0, len = initCount; i < len; i++) {
                let obj = new typeConstructor();
                this._list.push(obj);
            }
        }

        public newOne(): T {
            let obj = this._list.pop();
            if (!obj) {
                obj = new this._typeConstructor();
            }
            delete obj[Pool.inPoolTag];
            return obj;
        }

        public deleteOne(obj: T) {
            if (!obj || obj[Pool.inPoolTag]) return;
            obj[Pool.inPoolTag] = true;
            this._list.push(obj);
        }
    }
}